<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>编辑值班内容</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <div class="mb-10">
          值班日期：
          <el-date-picker
              v-model="value1"
              type="date"
              :picker-options="pickerOptions1"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
              size="small"
          ></el-date-picker>
        </div>
        <table width="100%" border="1">
          <template v-for="item_list in dutyListOption">
            <tr v-for="(list_tr, index_tr) in item_list.list" :key="list_tr.id">
              <td :rowspan="item_list.list.length" v-if="!index_tr">{{ item_list.name }}</td>
              <td>{{ list_tr.name }}</td>
              <td class="td-80">
                <el-rate v-model="list_tr.score" :colors="['#f18054', '#fa5901', '#ff0000']"></el-rate>
              </td>
            </tr>
          </template>
          <tr>
            <td colspan="2">亮点或存在问题</td>
            <td class="td-80">
              <el-input
                  type="textarea"
                  placeholder="亮点或存在问题"
                  v-model="problem"
                  maxlength="100"
                  show-word-limit
                  rows="5"
              >
              </el-input>
            </td>
          </tr>
          <tr>
            <td colspan="2">群众反馈</td>
            <td class="td-80">
              <el-input
                  type="textarea"
                  placeholder="群众反馈"
                  v-model="reflect"
                  maxlength="100"
                  show-word-limit
                  rows="5"
              >
              </el-input>
            </td>
          </tr>
        </table>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click.native="closePOpup" size="small">取消</el-button>
        <el-button size="small" type="primary" @click="checkEditFn">确定保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {dutySignAdd, dutyOptionIndex} from "@/api/admin/duty"
import {yeartime} from '@/assets/js/common'

export default {
  props: ['visible'],
  components: {},
  data() {
    return {
      value1: yeartime(new Date()),
      dialogVisible: false,
      dutyListOption: [],
      reflect: '',
      problem: '',
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() > Date.now() //减去就是不包含今天的 //  - 8.64e7 减去就是不包含今天的
        }
      },
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        this.dutyList()
      } else {
        this.dutyListOption = []
        this.reflect = ''
        this.problem = ''
      }
    }
  },
  created() {
  },
  methods: {
    dutyList() {
      dutyOptionIndex({p: 0}).then(res => {
        let a_ = {}
        for (let item of res.list) {
          if (!item.parent_id) {
            if (a_[item.id]) {
              a_[item.id].name = item.name
              a_[item.id].id = item.id
            } else {
              a_[item.id] = {
                name: item.name,
                id: item.id,
                list: []
              }
            }
          } else {
            if (a_[item.parent_id]) {
              a_[item.parent_id].list.push(item)
            } else {
              a_[item.parent_id] = {
                name: '',
                id: '',
                list: [item]
              }
            }
          }
        }
        for (let index in a_) {
          this.dutyListOption.push(a_[index])
        }
        console.log('值班内容', res, a_, this.dutyListOption)
      })
    },
    checkEditFn() {
      if(!this.value1){
        this.$message.error('选择值班日期')
        return
      }
      let data_ = []
      for (let item of this.dutyListOption) {
        for (let list of item.list) {
          data_.push({
            id: list.id,
            parent_id: list.parent_id,
            name: list.name,
            score: list.score || 0
          })
        }
      }
      dutySignAdd({list: JSON.stringify(data_), problem: this.problem, reflect: this.reflect, day: this.value1}).then(res => {
        console.log('res:', res)
        if (!res.code) {
          this.$message.success(res.msg)
          this.$emit('InitFn')
          this.closePOpup()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 720px;
  height: 720px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 5px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.jobTime span {
  padding: 0 10px;
}

table {
  border-collapse: collapse;
}

table th {
  height: 50px;
}

table td {
  padding: 5px;
  text-align: center;
  font-size: 12px;
}

table td em {
  font-style: normal;
  font-size: 16px;
}

table td i {
  font-style: normal;
  font-size: 12px;
}

.td-80 {
  width: 340px;
}
</style>


